﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_user_edit : System.Web.UI.Page
{
    public Cstudy.MODEL.users user;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int uid;
            if (int.TryParse(Request.QueryString["uid"],out uid))
            {
                LoadUser(uid);
                LoadInfo();
            }
        }
    }

    /// <summary>
    /// 查询用户
    /// </summary>
    /// <param name="uid">用户ID</param>
    /// <returns></returns>
    protected DataSet SelectUser(int uid)
    {
        return new Cstudy.BLL.Users().SelectUserByID(uid);
    }

    /// <summary>
    /// 加载用户实体
    /// </summary>
    /// <param name="uid"></param>
    protected void LoadUser(int uid)
    {
        DataRow dr = SelectUser(uid).Tables[0].Rows[0];
        user = new Cstudy.MODEL.users();
        if (dr != null)
        {
            user = new Cstudy.Tools.LoadModel().LoadUser(dr);
        }
    }

    /// <summary>
    /// 加载用户信息
    /// </summary>
    protected void LoadInfo()
    {
        NickName.Text = user.NickName;
        SexList.SelectedIndex = (int)user.Sex;
        ScoreBox.Text = user.UserScore.ToString();
        InfoBox.Text = user.Info;
        BirthBox.Text = user.Birth;
    }
    protected void butYes_Click(object sender, EventArgs e)
    {
        EditUser();
    }
    /// <summary>
    /// 修改用户
    /// </summary>
    protected void EditUser()
    {
        if (PwdBox.Text.Trim().Length > 0)
        {
            if (new Cstudy.Tools.RegexTools().IsValidByte(PwdBox.Text, 5, 20))
            {
                user.Pwd = new Cstudy.Tools.Tools().MakeMD5(PwdBox.Text);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('密码格式不正确！')", true);
                return;
            }
            user.NickName = NickName.Text;
            user.Sex = (Cstudy.MODEL.users.UserSex)SexList.SelectedIndex;
            int score;
            if (int.TryParse(ScoreBox.Text, out score))
            {
                user.UserScore = score;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('积分格式不正确！必须为数字')", true);
                return ;
            }
            user.Info = InfoBox.Text;
            if (new Cstudy.Tools.RegexTools().IsValidDate(BirthBox.Text))
            {
                user.Birth = BirthBox.Text;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('日期格式不正确！')", true);
                return;
            }
            if (new Cstudy.BLL.Users().UpdateUser(user))
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "success", "alert('用户修改成功！')", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "error", "alert('修改用户失败！')", true);
            }
        }
    }
}